#!/bin/bash

if [ $# -ne "1" ]
then
    echo "USO: ${0} <nombre_cliente> "
    exit 0
fi

NOMBRE_CLIENTE=${1}

#Redes y mascaras para el ruteo
RED_A="10.9.3.0"
RED_B="192.168.21.0"
RED_C="10.4.2.160"
RED_D="10.4.1.128"
RED_G="10.9.3.128"
RED_H="10.4.2.164"
RED_I="10.9.3.64"
RED_J="10.4.2.144"
RED_K="10.9.3.96"
RED_L="10.9.2.0"
RED_M1="172.43.0.64"
RED_M2="172.43.0.68"
RED_M3="172.43.0.72"
RED_M4="172.43.0.76"
RED_M5="172.43.0.80"
RED_M6="172.43.0.84"
RED_N="10.4.2.168"
RED_O="10.4.2.172"
RED_P="10.4.2.176"
RED_Q="10.4.2.128"
MASK_24="255.255.255.0"
MASK_25="255.255.255.128"
MASK_26="255.255.255.192"
MASK_27="255.255.255.224"
MASK_28="255.255.255.240"
MASK_29="255.255.255.248"
MASK_30="255.255.255.252"

#Borra el  gateway existente
sudo route del default
if [ "${NOMBRE_CLIENTE}" == "HostA" ]
then
	TAP="tap64"
	sudo route add -net "${RED_B}" netmask "${MASK_24}" gw "10.9.3.6" dev "${TAP}"
	sudo route add -net "${RED_C}" netmask "${MASK_30}" gw "10.9.3.2" dev "${TAP}"
	sudo route add -net "${RED_D}" netmask "${MASK_25}" gw "10.9.3.2" dev "${TAP}"
	sudo route add -net "${RED_G}" netmask "${MASK_25}" gw "10.9.3.2" dev "${TAP}"
	sudo route add -net "${RED_H}" netmask "${MASK_30}" gw "10.9.3.2" dev "${TAP}"
	sudo route add -net "${RED_I}" netmask "${MASK_27}" gw "10.9.3.6" dev "${TAP}"
	sudo route add -net "${RED_J}" netmask "${MASK_28}" gw "10.9.3.2" dev "${TAP}"
	sudo route add -net "${RED_K}" netmask "${MASK_27}" gw "10.9.3.2" dev "${TAP}"
	sudo route add -net "${RED_L}" netmask "${MASK_24}" gw "10.9.3.2" dev "${TAP}"
	sudo route add -net "${RED_M4}" netmask "${MASK_30}" gw "10.9.3.1" dev "${TAP}"
	sudo route add -net "${RED_M5}" netmask "${MASK_30}" gw "10.9.3.1" dev "${TAP}"
	sudo route add -net "${RED_M6}" netmask "${MASK_30}" gw "10.9.3.1" dev "${TAP}"
	sudo route add -net "${RED_Q}" netmask "${MASK_28}" gw "10.9.3.2" dev "${TAP}"

elif [ "${NOMBRE_CLIENTE}" == "HostB" ]
then
	TAP="tap65"
	sudo route add -net "${RED_A}" netmask "${MASK_26}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_B}" netmask "${MASK_24}" gw "10.9.3.97" dev "${TAP}"
	sudo route add -net "${RED_C}" netmask "${MASK_30}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_D}" netmask "${MASK_25}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_G}" netmask "${MASK_25}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_H}" netmask "${MASK_30}" gw "10.9.3.97" dev "${TAP}"
	sudo route add -net "${RED_I}" netmask "${MASK_27}" gw "10.9.3.97" dev "${TAP}"
	sudo route add -net "${RED_J}" netmask "${MASK_28}" gw "10.9.3.100" dev "${TAP}"
	sudo route add -net "${RED_L}" netmask "${MASK_24}" gw "10.9.3.97" dev "${TAP}"
	sudo route add -net "${RED_M1}" netmask "${MASK_30}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_M2}" netmask "${MASK_30}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_M3}" netmask "${MASK_30}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_M4}" netmask "${MASK_30}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_M5}" netmask "${MASK_30}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_M6}" netmask "${MASK_30}" gw "10.9.3.98" dev "${TAP}"
	sudo route add -net "${RED_Q}" netmask "${MASK_28}" gw "10.9.3.97" dev "${TAP}"

elif [ "${NOMBRE_CLIENTE}" == "HostC" ]
then
	TAP="tap66"
	sudo route add -net "${RED_A}" netmask "${MASK_26}" gw "10.4.2.130" dev "${TAP}"
	sudo route add -net "${RED_B}" netmask "${MASK_24}" gw "10.4.2.133" dev "${TAP}"
	sudo route add -net "${RED_C}" netmask "${MASK_30}" gw "10.4.2.130" dev "${TAP}"
	sudo route add -net "${RED_D}" netmask "${MASK_25}" gw "10.4.2.130" dev "${TAP}"
	sudo route add -net "${RED_G}" netmask "${MASK_25}" gw "10.4.2.131" dev "${TAP}"
	sudo route add -net "${RED_H}" netmask "${MASK_30}" gw "10.4.2.132" dev "${TAP}"
	sudo route add -net "${RED_I}" netmask "${MASK_27}" gw "10.4.2.132" dev "${TAP}"
	sudo route add -net "${RED_J}" netmask "${MASK_28}" gw "10.4.2.132" dev "${TAP}"
	sudo route add -net "${RED_L}" netmask "${MASK_24}" gw "10.4.2.132" dev "${TAP}"
	sudo route add -net "${RED_M1}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAP}"
	sudo route add -net "${RED_M2}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAP}"
	sudo route add -net "${RED_M3}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAP}"
	sudo route add -net "${RED_M4}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAP}"
	sudo route add -net "${RED_M5}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAP}"
	sudo route add -net "${RED_M6}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAP}"
	sudo route add -net "${RED_K}" netmask "${MASK_27}" gw "10.4.2.132" dev "${TAP}"

elif [ "${NOMBRE_CLIENTE}" == "WEBSERVER" ]
then
    TAP="tap118"

    #ruteo de B
        sudo route add -net "${RED_A}" netmask "${MASK_26}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_Q}" netmask "${MASK_28}" gw "192.168.21.3" dev "${TAP}"
	sudo route add -net "${RED_C}" netmask "${MASK_30}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_D}" netmask "${MASK_25}" gw "192.168.21.3" dev "${TAP}"
	sudo route add -net "${RED_G}" netmask "${MASK_25}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_H}" netmask "${MASK_30}" gw "192.168.21.3" dev "${TAP}"
	sudo route add -net "${RED_I}" netmask "${MASK_27}" gw "192.168.21.3" dev "${TAP}"
	sudo route add -net "${RED_J}" netmask "${MASK_28}" gw "192.168.21.3" dev "${TAP}"
	sudo route add -net "${RED_L}" netmask "${MASK_24}" gw "192.168.21.3" dev "${TAP}"
	sudo route add -net "${RED_M1}" netmask "${MASK_30}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_M2}" netmask "${MASK_30}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_M3}" netmask "${MASK_30}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_M4}" netmask "${MASK_30}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_M5}" netmask "${MASK_30}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_M6}" netmask "${MASK_30}" gw "192.168.21.4" dev "${TAP}"
	sudo route add -net "${RED_K}" netmask "${MASK_27}" gw "192.168.21.3" dev "${TAP}"

elif [ "${NOMBRE_CLIENTE}" == "FTPSERVER" ]
then
    TAP="tap119"

    #ruteo de L
        sudo route add -net "${RED_A}" netmask "${MASK_26}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_Q}" netmask "${MASK_28}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_C}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_D}" netmask "${MASK_25}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_G}" netmask "${MASK_25}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_H}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_I}" netmask "${MASK_27}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_J}" netmask "${MASK_28}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_B}" netmask "${MASK_24}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_M1}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_M2}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_M3}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_M4}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_M5}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_M6}" netmask "${MASK_30}" gw "10.9.2.4" dev "${TAP}"
	sudo route add -net "${RED_K}" netmask "${MASK_27}" gw "10.9.2.4" dev "${TAP}"

elif [ "${NOMBRE_CLIENTE}" == "TELSERVER" ]
then
    TAPD="tap321"
    TAPQ="tap654"

    #ruteo de D. Todos van por R7 (unico)
        sudo route add -net "${RED_A}" netmask "${MASK_26}" gw "10.4.1.129" dev "${TAPD}" metric 1
	sudo route add -net "${RED_Q}" netmask "${MASK_28}" gw "10.4.1.129" dev "${TAPD}" metric 1
	sudo route add -net "${RED_C}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 1
	sudo route add -net "${RED_L}" netmask "${MASK_24}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_G}" netmask "${MASK_25}" gw "10.4.1.129" dev "${TAPD}" metric 1
	sudo route add -net "${RED_H}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_I}" netmask "${MASK_27}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_J}" netmask "${MASK_28}" gw "10.4.1.129" dev "${TAPD}" metric 1
	sudo route add -net "${RED_B}" netmask "${MASK_24}" gw "10.4.1.129" dev "${TAPD}" metric 1
	sudo route add -net "${RED_M1}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_M2}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_M3}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_M4}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_M5}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_M6}" netmask "${MASK_30}" gw "10.4.1.129" dev "${TAPD}" metric 10
	sudo route add -net "${RED_K}" netmask "${MASK_27}" gw "10.4.1.129" dev "${TAPD}" metric 10


    #ruteo de Q.
        sudo route add -net "${RED_A}" netmask "${MASK_26}" gw "10.4.2.130" dev "${TAPQ}" metric 10
	sudo route add -net "${RED_D}" netmask "${MASK_25}" gw "10.4.2.130" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_C}" netmask "${MASK_30}" gw "10.4.2.130" dev "${TAPQ}" metric 10
	sudo route add -net "${RED_L}" netmask "${MASK_25}" gw "10.4.2.132" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_G}" netmask "${MASK_25}" gw "10.4.2.131" dev "${TAPQ}" metric 10
	sudo route add -net "${RED_H}" netmask "${MASK_30}" gw "10.4.2.132" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_I}" netmask "${MASK_27}" gw "10.4.2.132" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_J}" netmask "${MASK_28}" gw "10.4.2.132" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_B}" netmask "${MASK_24}" gw "10.4.2.133" dev "${TAPQ}" metric 10
	sudo route add -net "${RED_M1}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_M2}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_M3}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_M4}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_M5}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_M6}" netmask "${MASK_30}" gw "10.4.2.131" dev "${TAPQ}" metric 1
	sudo route add -net "${RED_K}" netmask "${MASK_27}" gw "10.4.2.132" dev "${TAPQ}" metric 1
else
	echo "El primer parametro debe ser: 'HostA', 'HostB', 'HostC', 'WEBSERVER', 'FTPSERVER' o 'TELSERVER'"
	exit 1
fi

exit 0


